package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.dto.DistributionProdDto;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.service.DistributionProdService;
import com.yami.shop.distribution.common.service.DistributionUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/distribution/prod")
@Tag(name = "分销商品接口")
public class DistributionProdController {

    @Autowired
    private DistributionProdService distributionProdService;
    @Autowired
    private DistributionUserService distributionUserService;

    @GetMapping("/page")
    @Operation(summary =  "分页查看分销商品", description = "根据店铺和分销员id获取")
    @Parameters({
            @Parameter(name = "shopId", description = "店铺id"),
            @Parameter(name = "prodName", description = "商品名"),
            @Parameter(name = "sort", description = "排序(1时间排序 2销量排序 3价格排序) 默认1"),
            @Parameter(name = "orderBy", description = "排序(0升序 1降序) 默认1"),
    })
    public ServerResponseEntity<IPage<DistributionProdDto>> page(@RequestParam(name="shopId", required = false, defaultValue = "0") Long shopId,
                                                           @RequestParam(name="prodName") String prodName,
                                                           @RequestParam(name="sort", defaultValue = "1") Integer sort,
                                                           @RequestParam(name="orderBy", defaultValue = "1") Integer orderBy,
                                                           PageParam<DistributionProdDto> page) {
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        page.setRecords(distributionProdService.distributionProdDtoPage(page, prodName, sort, orderBy, distributionUser));
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/isDistributionProd")
    @Operation(summary =  "根据商品id查看该商品是否为分销商品", description = "根据商品id获取")
    @Parameter(name = "prodId", description = "商品id", required = true)
    public ServerResponseEntity<Boolean> isDistributionProd(Long prodId) {
        return ServerResponseEntity.success(distributionProdService.getDistributionProdPoByProdId(prodId) != null);
    }

    @GetMapping("/isStateByProdId")
    @Operation(summary =  "根据商品id与状态查看该商品是处于该状态", description = "根据商品id与状态查看该商品是处于该状态")
    @Parameters({
            @Parameter(name = "prodId", description = "商品id", required = true),
            @Parameter(name = "state", description = "分销商品状态", required = true)
    })
    public ServerResponseEntity<Boolean> isStateByProdId(@RequestParam(name="prodId") Long prodId,
                                                   @RequestParam(name="state") Integer state) {
        return ServerResponseEntity.success(distributionProdService.isStateByProdId(prodId, state));
    }
}
